<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="searchForm">
        <el-form-item>
          <el-input v-model="searchForm.name" placeholder="领养订单"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getAdoptOrders">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="adoptOrders" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="digest" label="订单摘要">
      </el-table-column>
      <el-table-column prop="orderSn" label="订单号">
      </el-table-column>
      <el-table-column prop="price" label="价格" width="80">
      </el-table-column>
      <el-table-column prop="state" label="状态" width="80">
        <template scope="scope">
          <span v-if="scope.row.state==0" style="color: purple">待支付</span>
          <span v-if="scope.row.state==1" style="color: blue">待确认</span>
          <span v-if="scope.row.state==2" style="color: green">完成</span>
          <span v-if="scope.row.state==(-1)" style="color: red">取消</span>
        </template>
      </el-table-column>
      <el-table-column prop="serviceMethod" label="服务方式">
        <template scope="scope">
          <span v-if="scope.row.serviceMethod==0" style="color: goldenrod">送宠上门</span>
        </template>
      </el-table-column>
      <el-table-column prop="petId" label="宠物名称">
      </el-table-column>
      <el-table-column prop="userId" label="下单用户名">
      </el-table-column>
      <el-table-column prop="shopId" label="店铺">
      </el-table-column>
      <el-table-column prop="addressId" label="配送区域|详细地址">
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template scope="scope" >
          <el-button size="small" v-if="scope.row.state==1" @click="handleEdit(scope.$index, scope.row)">确认配送</el-button>
          <el-button type="danger" v-if="scope.row.state==0" size="small" @click="handleDel(scope.$index, scope.row)">取消订单</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total"
                     style="float:right;">
      </el-pagination>
    </el-col>


  </section>
</template>

<script>
import {quillEditor} from "vue-quill-editor"; //调用编辑器
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
import quillConfig from '../../common/js/quill-config.js'

export default {
  components: {
    quillEditor
  },
  data() {
    return {
      quillOption: quillConfig,
      fileList: [],
      title: '',
      searchForm: {
        name: ''
      },
      adoptOrders: [],
      total: 0,
      page: 1,
      listLoading: false,
      sels: [],//列表选中列

      editFormVisible: false,//编辑界面是否显示
      editLoading: false,
      editFormRules: {
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ]
      },
      addLoading: false,
      addFormRules: {
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ]
      }

    }
  },
  methods: {
    handleSuccess(response, file, fileList) {
      console.log("===========")
      console.log(fileList);
      //resultObj 文件的服务器地址，放到数据库中
      //根据双向绑定 logo 就有值 1,2,3
      let resourcesTmp = '';
      if (fileList != null && fileList.length > 0) {
        for (var i = 0; i < fileList.length; i++) {
          if (i == 0) {
            resourcesTmp = resourcesTmp + fileList[i].response.resultObject;
          } else {
            resourcesTmp = resourcesTmp + "," + fileList[i].response.resultObject;
          }
        }
      }
      this.editForm.resources = resourcesTmp;
      console.log(this.editForm.resources)
    },
    handleRemove(file, fileList) {
      var path = file.response.resultObject;
      console.log(fileList)
      this.$http.get( "/fast_dfs?path="+path)
          .then(res => {
            if (res.data.success) {
              this.$message({
                message: '删除成功!',
                type: 'success'
              });
            } else {
              this.$message({
                message: '删除失败!',
                type: 'error'
              });
            }
          })

      //删除图片后移除data里面数据
      let resourcesTmp = '';
      if (fileList != null && fileList.length > 0) {
        for (var i = 0; i < fileList.length; i++) {
          if (i == 0) {
            resourcesTmp = resourcesTmp + fileList[i].response.resultObj;
          } else {
            resourcesTmp = resourcesTmp + "," + fileList[i].response.resultObj;
          }
        }
      }
      this.editForm.resources = resourcesTmp;
      console.log(this.editForm.resources);
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getAdoptOrders();
    },
    //获取领养订单列表
    getAdoptOrders() {
      let para = {
        currentPage:this.page,
        pageSize: 10,
        keyword: this.searchForm.name
      };
      //打开加载的效果
      this.listLoading = true;
      //
      this.$http.post("/orderAdopt/list", para).then(res => {
       console.log(res.data.data)
        this.adoptOrders = res.data.data;
        this.total = res.data.totals;
        //关闭加载的效果
        this.listLoading = false;
      });
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        //开启加载的模态框
        this.listLoading = true;
        //NProgress.start();
        console.debug(row)
        //后台地址的请求
        this.$http.delete("/pet/" + row.id).then((res) => {
          this.listLoading = false;
          console.debug(res);
          if (res.data.success) {
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          }

          this.getAdoptOrders();
        });
      }).catch(() => {

      });
    },
    //编辑
    editSubmit: function () {
      //验证当前的表单是否符合验证规则
      this.$refs.editForm.validate((valid) => {

        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            //NProgress.start();
            //把表单的数据组装为json
            let para = Object.assign({}, this.editForm);

            //para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
            this.$http.put('/pet', para).then((res) => {
              this.editLoading = false;
              //NProgress.done();
              this.$message({
                message: '提交成功',
                type: 'success'
              });
              //重置表单-调用现成的方法
              this.$refs['editForm'].resetFields();
              //关闭 加载框
              this.editFormVisible = false;
              //重新刷新页面数据
              this.getAdoptOrders();
            });
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        let para = {ids: ids};
        this.$http.post('/pet/dels', ids).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getAdoptOrders();
        });
      }).catch(() => {

      });
    }
  },
  mounted() {
    //当页面加载完以后。执行的方法
    this.getAdoptOrders();
  }
}

</script>

<style scoped>

</style>